package com.luban.service;

import com.luban.dao.OrderMapper;
import com.luban.dao.UserMapper;
import com.luban.entity.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.TimeUnit;

/**
 * @Value(value = "${orderService}")  表示占位符，从Environment里面获取值
 *
 * 当前线程上面是不是有事务等价于当前线程上面是不是有数据库连接
 *
 *
 * mysql不支持提交到某个安全点，只能回滚到某个安全点
 *
 * 以非事务方式运行底层原理：不使用Spring获取的Connection
 *
 * 嵌套事务的超时时间继承自外层事务，确保整体事务在父级设定的时间内完成。
 *
 */
@Component //这样注册的bean的BeanDefinition的destroyMethodName为空
public class UserService /*implements UserInterface*/ { //BeanDefinition beanClass="com.luban.UserService"  ASM --->  Class

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private UserService userService;

	/**
	 * 查看数据库连接：show status like 'Thread%';
	 * 查看数据库是否开启自动提交：show variables like 'auto%';
	 */
	@Transactional(timeout = 3)
	public void test() {
		//mybatis  ----->   Spring创建连接的
		userMapper.insert1();

		//TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
		//
		//	@Override
		//	public void suspend() {
		//		System.out.println("被挂起了");
		//	}
		//});

		try {
			TimeUnit.SECONDS.sleep(4);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		userService.a();

		System.out.println("123");
	}


	//@Transactional(timeout = 4)
	public void a() {

		userMapper.insert2();

		//try {
		//	TimeUnit.SECONDS.sleep(4);
		//} catch (InterruptedException e) {
		//	e.printStackTrace();
		//}
	}



	//@Autowired
	//private ApplicationContext applicationContext;


	//@Autowired
	//private Environment environment;


	//@PreDestroy
	//public void destory() {
	//	System.out.println("destory");
	//}


	//@PostConstruct
	//public void init() {
	//	System.out.println("init");
	//}

	//public UserService(User user, User user1, UserInterface user2) {
	//	System.out.println("接口");
	//}

	//public UserService(User user, User user1, User user2) {
	//	System.out.println("三个");
	//}


	//public UserService(User user) {
	//	System.out.println("一个");
	//}
	//
	//public UserService() {
	//	System.out.println("无参");
	//}


	//public void test() {
	//	System.out.println("111");
	//}

	//@Override
	//public void destroy() throws Exception {
	//	System.out.println("destory");
	//}

	//public void close() {
	//	System.out.println("close");
	//}

	//autowired=byType,byName 这个方法执行一次，初始化前
	//autowired=no 执行两次，@Autowired，初始化前
	//@Override
	//@Autowired
	//public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	//	System.out.println("xxx");
	//}
}
